<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/5/6
 * Time: 15:32
 */
namespace Service\Controller;
use Admin\Controller\AdminController;

class RepairTypeController extends AdminController{

    public function lst(){
        if(IS_POST){
            $pageSize = I('post.pageSize','0','intval');
            $pageIndex = $pageSize * I('post.pageIndex','20','intval');

            $type = D("RepairType")->order('sort')->select();
            $type = D("RepairType")->getPName($type);
            $data_count = D("RepairType")->count();
            $type = \Org\Util\Tree::create($type);

//            myDump($type);die;
            $data['data'] = $type;
            $data['recordsTotal'] = $data_count;
            $data['recordsFiltered'] = $data_count;
            $this->ajaxReturn($data);

        }else{
            $this->display();
        }
    }

    public function add(){
        $model = D("RepairType");
        if(IS_POST){
            if($ok = $model->create())
            {
                $model->add();
            }else{
                $this->error($model->getError());
            }
        }else{


            $where['level'] = array('lt',3);
            $type = M("RepairType")->where($where)->order('sort')->select();
            $type = \Org\Util\Tree::create($type);

            $this->assign("type",$type);
            $this->display();
        }
    }

    public function edit(){
        $repairModel = D("RepairType");
        if(IS_POST){
            if($model = $repairModel->create())
            {
                if($model['id']){
                    $repairModel->save();
                }else{
                    $this->error("找不到该类型");
                }
            }else{
                $this->error($repairModel->getError());
            }
        }else{
            $id = I("get.id",0,"intval");
            if($id){
                $the_type = $repairModel->find($id);
            }else{
                $this->error("找不到该类型");
            }
            $where['level'] = array('lt',3);
            $type = M("RepairType")->where($where)->order('sort')->select();
            $type = \Org\Util\Tree::create($type);

            $this->assign("the_type",$the_type);
            $this->assign("type",$type);
            $this->display();
        }
    }

    public function delete(){
        if(IS_POST){
            $ids = I("post.id");
            $id_arr = explode(",", $ids);
            $model = D("RepairType");
            foreach ($id_arr as $id) {
                $ret = $model->delete($id);
            }
            echo 1;
        }
    }

    public function input()
    {
        $model = D('RepairType');
        $req = $this->getReq();
        $path = $req['path'];
        $sheetNo = $req['sheetNo'] ? $req['sheetNo'] : 0;
        $startRow = $req['startRow'] ? $req['startRow'] : 1;
        $excelOffice = new \IDCU\Office\ExcelOffice();
        $arr = $excelOffice->getArr($path, $sheetNo, $startRow);
        foreach ($arr as $v) {
            $has_cate = $model->findName($v['cate']);
            if($has_cate){
                $has_type = $model->findName($v['type'],$has_cate['id']);
                if($has_type){

                }else{
                    $price_data = explode('/',$v['price']);
                    $data = array();
                    $data['name'] = $v['type'];
                    $data['pid'] = $has_cate['id'];
                    $data['level'] = 2;
                    $data['price'] = $price_data[0];
                    $data['unit'] = $price_data[1];
                    $model->add($data);
                }
            }else {
                $data = array();
                $data['name'] = $v['cate'];
                $data['level'] = 1;
                $pid = $model->add($data);

                $price_data = explode('/',$v['price']);
                $data = array();
                $data['name'] = $v['type'];
                $data['pid'] = $pid;
                $data['level'] = 2;
                $data['price'] = $price_data[0];
                $data['unit'] = $price_data[1];
                $model->add($data);
            }
        }
//        myDump($arr);die;
    }

    public function getLevel1(){
        $data = D('RepairType')->where(array('level'=>1))->order('sort asc')->select();
        $this->ajaxReturn($data);
    }
    public function getLevel2(){
        $id = I('tid');
        $data = D('RepairType')->where(array('level'=>2,'pid'=>$id))->order('sort asc')->select();
        $this->ajaxReturn($data);
    }
}